home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / stonehenge / rgb.c++ < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  255 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <stdio.h>
  39. #include <stdlib.h> 
  40. #include <string.h>
  41. #include <GL/gl.h>
  42. #include "rgb.h"
  43.  
  44. /******************************************************************************/
  45.  
  46. typedef struct _rawImageRec {
  47.     unsigned short imagic;
  48.     unsigned short type;
  49.     unsigned short dim;
  50.     unsigned short sizeX, sizeY, sizeZ;
  51.     unsigned long min, max;
  52.     unsigned long wasteBytes;
  53.     char name[80];
  54.     unsigned long colorMap;
  55.     FILE *file;
  56.     unsigned char *tmp, *tmpR, *tmpG, *tmpB;
  57.     unsigned long rleEnd;
  58.     GLuint *rowStart;
  59.     GLint *rowSize;
  60. } rawImageRec;
  61.  
  62. /******************************************************************************/
  63.  
  64. static void ConvertShort(unsigned short *array, long length)
  65. {
  66.     unsigned long b1, b2;
  67.     unsigned char *ptr;
  68.  
  69.     ptr = (unsigned char *)array;
  70.     while (length--) {
  71.     b1 = *ptr++;
  72.     b2 = *ptr++;
  73.     *array++ = (b1 << 8) | (b2);
  74.     }
  75. }
  76.  
  77. static void ConvertLong(GLuint *array, long length)
  78. {
  79.     unsigned long b1, b2, b3, b4;
  80.     unsigned char *ptr;
  81.  
  82.     ptr = (unsigned char *)array;
  83.     while (length--) {
  84.     b1 = *ptr++;
  85.     b2 = *ptr++;
  86.     b3 = *ptr++;
  87.     b4 = *ptr++;
  88.     *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
  89.     }
  90. }
  91.  
  92. static rawImageRec *RawImageOpen(char *fileName)
  93. {
  94.     union {
  95.     int testWord;
  96.     char testByte[4];
  97.     } endianTest;
  98.     rawImageRec *raw;
  99.     GLenum swapFlag;
  100.     int x;
  101.  
  102.     endianTest.testWord = 1;
  103.     if (endianTest.testByte[0] == 1) {
  104.     swapFlag = GL_TRUE;
  105.     } else {
  106.     swapFlag = GL_FALSE;
  107.     }
  108.  
  109.     raw = (rawImageRec *)malloc(sizeof(rawImageRec));
  110.     if (raw == NULL) {
  111.     return NULL;
  112.     }
  113.     if ((raw->file = fopen(fileName, "rb")) == NULL) {
  114.     return NULL;
  115.     }
  116.  
  117.     fread(raw, 1, 12, raw->file);
  118.  
  119.     if (swapFlag) {
  120.     ConvertShort(&raw->imagic, 6);
  121.     }
  122.  
  123.     raw->tmp = (unsigned char *)malloc(raw->sizeX*256);
  124.     raw->tmpR = (unsigned char *)malloc(raw->sizeX*256);
  125.     raw->tmpG = (unsigned char *)malloc(raw->sizeX*256);
  126.     raw->tmpB = (unsigned char *)malloc(raw->sizeX*256);
  127.     if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL ||
  128.     raw->tmpB == NULL) {
  129.     return NULL;
  130.     }
  131.  
  132.     if ((raw->type & 0xFF00) == 0x0100) {
  133.     x = raw->sizeY * raw->sizeZ * sizeof(GLuint);
  134.     raw->rowStart = (GLuint *)malloc(x);
  135.     raw->rowSize = (GLint *)malloc(x);
  136.     if (raw->rowStart == NULL || raw->rowSize == NULL) {
  137.         return NULL;
  138.     }
  139.     raw->rleEnd = 512 + (2 * x);
  140.     fseek(raw->file, 512, SEEK_SET);
  141.     fread(raw->rowStart, 1, x, raw->file);
  142.     fread(raw->rowSize, 1, x, raw->file);
  143.     if (swapFlag) {
  144.         ConvertLong(raw->rowStart, x/sizeof(GLuint));
  145.         ConvertLong((GLuint *)raw->rowSize, x/sizeof(GLint));
  146.     }
  147.     }
  148.     return raw;
  149. }
  150.  
  151. static void RawImageClose(rawImageRec *raw)
  152. {
  153.  
  154.     fclose(raw->file);
  155.     free(raw->tmp);
  156.     free(raw->tmpR);
  157.     free(raw->tmpG);
  158.     free(raw->tmpB);
  159.     free(raw);
  160. }
  161.  
  162. static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z)
  163. {
  164.     unsigned char *iPtr, *oPtr, pixel;
  165.     int count;
  166.  
  167.     if ((raw->type & 0xFF00) == 0x0100) {
  168.     fseek(raw->file, raw->rowStart[y+z*raw->sizeY], SEEK_SET);
  169.     fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY],
  170.           raw->file);
  171.  
  172.     iPtr = raw->tmp;
  173.     oPtr = buf;
  174.     while (1) {
  175.         pixel = *iPtr++;
  176.         count = (int)(pixel & 0x7F);
  177.         if (!count) {
  178.         return;
  179.         }
  180.         if (pixel & 0x80) {
  181.         while (count--) {
  182.             *oPtr++ = *iPtr++;
  183.         }
  184.         } else {
  185.         pixel = *iPtr++;
  186.         while (count--) {
  187.             *oPtr++ = pixel;
  188.         }
  189.         }
  190.     }
  191.     } else {
  192.     fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY),
  193.           SEEK_SET);
  194.     fread(buf, 1, raw->sizeX, raw->file);
  195.     }
  196. }
  197.  
  198. static void RawImageGetData(rawImageRec *raw, RGBImageRec *final)
  199. {
  200.     unsigned char *ptr;
  201.     int i, j;
  202.  
  203.     final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4);
  204.     if (final->data == NULL) {
  205.     return;
  206.     }
  207.  
  208.     ptr = final->data;
  209.     for (i = 0; i < raw->sizeY; i++) {
  210.     RawImageGetRow(raw, raw->tmpR, i, 0);
  211.     RawImageGetRow(raw, raw->tmpG, i, 1);
  212.     RawImageGetRow(raw, raw->tmpB, i, 2);
  213.     for (j = 0; j < raw->sizeX; j++) {
  214.         *ptr++ = *(raw->tmpR + j);
  215.         *ptr++ = *(raw->tmpG + j);
  216.         *ptr++ = *(raw->tmpB + j);
  217.     }
  218.     }
  219. }
  220.  
  221. RGBImageRec *rgbImageLoad(char *fileName)
  222. {
  223.     rawImageRec *raw;
  224.     RGBImageRec *final;
  225.  
  226.     raw = RawImageOpen(fileName);
  227.     if (raw == NULL) {
  228.     return NULL;
  229.     }
  230.  
  231.     final = (RGBImageRec *)malloc(sizeof(RGBImageRec));
  232.     if (final == NULL) {
  233.     RawImageClose(raw);
  234.     return NULL;
  235.     }
  236.     final->sizeX = raw->sizeX;
  237.     final->sizeY = raw->sizeY;
  238.  
  239.     RawImageGetData(raw, final);
  240.     RawImageClose(raw);
  241.  
  242.     return final;
  243. }
  244.  
  245. void rgbImageFree(RGBImageRec *img)
  246. {
  247.     if (img) {
  248.     if (img->data)
  249.         free(img->data);
  250.     free(img);
  251.     }
  252. }
  253.  
  254. /******************************************************************************/
  255.